package org.lemon.transport.context.service.queue.provider;

import org.lemon.transport.context.service.queue.ServiceType;
import org.lemon.transport.context.service.queue.TopicPartitionInfo;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class NotificationsTopicService {
    private Map<String, TopicPartitionInfo> coreNotificationTopics = new HashMap<>();
    private Map<String, TopicPartitionInfo> ruleEngineNotificationTopics = new HashMap<>();

    public TopicPartitionInfo getNotificationsTopic(ServiceType serviceType, String serviceId) {
        switch (serviceType) {
            case CORE:
                return coreNotificationTopics.computeIfAbsent(serviceId,
                        id -> buildNotificationsTopicPartitionInfo(serviceType, serviceId));
            case RULE_ENGINE:
                return ruleEngineNotificationTopics.computeIfAbsent(serviceId,
                        id -> buildNotificationsTopicPartitionInfo(serviceType, serviceId));
            default:
                return buildNotificationsTopicPartitionInfo(serviceType, serviceId);
        }
    }

    private TopicPartitionInfo buildNotificationsTopicPartitionInfo(ServiceType serviceType, String serviceId) {
        return new TopicPartitionInfo(serviceType.name().toLowerCase() + ".notifications." + serviceId, null, null, false);
    }
}
